package mn.more.wits.client;

import com.google.gwt.user.client.ui.*;
import mn.more.wits.client.dto.Handshake;
import mn.more.wits.client.dto.UserDTO;
import mn.more.wits.client.gui.StatusBar;
import mn.more.wits.client.gui.WitsButton;
import mn.more.wits.client.util.BrowserUtil;
import net.mygwt.ui.client.Registry;
import net.mygwt.ui.client.Style;
import net.mygwt.ui.client.event.BaseEvent;
import net.mygwt.ui.client.event.SelectionListener;
import net.mygwt.ui.client.widget.LoadingPanel;
import net.mygwt.ui.client.widget.MessageBox;
import net.mygwt.ui.client.widget.Viewport;
import net.mygwt.ui.client.widget.layout.CenterLayout;
import net.mygwt.ui.client.widget.layout.FillLayout;

/**
 * @author $Author: mikeliucc $
 * @version $Id: AppContext.java 5 2008-09-01 12:08:42Z mikeliucc $
 */
public final class AppContext {

	// todo: need to externalize
	public static final String APP_NAME = "wits";
	public static final String DEFAULT_STYLE_LABEL = "wits-label";
	public static final String DEFAULT_STYLE_RO_VALUE = "wits-value";

	private static Handshake handshake;
	private static UserDTO user;
	private static ExamTakerStartPage examTakerStartPage;

	private AppContext() { }

	public static Handshake getHandshake() { return handshake; }

	public static void setHandshake(Handshake handshake) { AppContext.handshake = handshake; }

	public static UserDTO getUser() { return AppContext.user; }

	public static void removeUser() { AppContext.user = null; }

	public static void setUser(UserDTO user) { AppContext.user = user; }

	public static void centerize(CellPanel p, Widget w) {
		p.setCellVerticalAlignment(w, HasVerticalAlignment.ALIGN_MIDDLE);
		p.setCellHorizontalAlignment(w, HasHorizontalAlignment.ALIGN_CENTER);
		p.setCellHeight(w, "100%");
		p.setCellWidth(w, "100%");
	}

	public static LoginPanel getLoginPanel() {
		LoginPanel loginPanel = (LoginPanel) Registry.get(LoginPanel.ID);
		if (loginPanel == null) { loginPanel = new LoginPanel(); }
		return loginPanel;
	}

	public static void showLoginPage() {
		showLoading();
		MainPanel main = getMainPanel();
		main.removeAll();
		main.hideLogout();
		main.setLayout(new CenterLayout());

		LoginPanel loginPanel = getLoginPanel();
		main.add(loginPanel);
		main.layout(true);
		hideLoading();

		loginPanel.resetForm();
	}

	public static void showStartPage() {
		showLoading();

		MainPanel main = getMainPanel();
		main.showLogout();

		String role = getUser().getMainRole();
		// todo: externalize role name
		if ("Exam Taker".equals(role)) {
			if (AppContext.examTakerStartPage == null) {
				AppContext.examTakerStartPage = new ExamTakerStartPage();
			} else {
				AppContext.examTakerStartPage.init();
			}

			main.removeAll();

			FillLayout layout = new FillLayout();
			layout.setSpacing(10);
			main.setLayout(layout);

			main.add(examTakerStartPage);
			main.layout(true);
		}

		hideLoading();
	}

	public static void showLoading() { LoadingPanel.get().show(AppMsg.Self.get().loading()); }

	public static void hideLoading() { LoadingPanel.get().hide(); }

	public static MainPanel getMainPanel() { return (MainPanel) Registry.get(MainPanel.ID); }

	public static Viewport getViewport() { return (Viewport) Registry.get("viewport"); }

	public static void setStatusMessage(String message) {
		StatusBar statusbar = (StatusBar) Registry.get(StatusBar.ID);
		statusbar.setStatus(message);
	}

	public static void setStatusMessage(String imgUrl, String message) {
		StatusBar statusbar = (StatusBar) Registry.get(StatusBar.ID);
		statusbar.setStatusImage(imgUrl);
		statusbar.setStatus(message);
	}

	public static void setStatusError(String error) {
		StatusBar statusbar = (StatusBar) Registry.get(StatusBar.ID);
		statusbar.setError(error);
	}

	public static void setStatusError(String imgUrl, String error) {
		StatusBar statusbar = (StatusBar) Registry.get(StatusBar.ID);
		statusbar.setStatusImage(imgUrl);
		statusbar.setError(error);
	}

	public static void setStatusError(String error, Throwable e) {
		StatusBar statusbar = (StatusBar) Registry.get(StatusBar.ID);
		statusbar.setError(error + " - " + e.getMessage());
	}

	public static void setStatusError(String imgUrl, String error, Throwable e) {
		StatusBar statusbar = (StatusBar) Registry.get(StatusBar.ID);
		statusbar.setStatusImage(imgUrl);
		statusbar.setError(error + " - " + e.getMessage());
	}

	/**
	 * standardize the creation of a spacer element.
	 * <p><b>VERY IMPORTANT</b>: The created spacer MUST not be reused; it can ONLY be placed once.
	 * All spacer should be created via this method. </p>
	 */
	public static HTML createSpacer() { return new HTML("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"); }

	public static MessageBox makeMessageBox(String title, int icon, int style, String message) {
		MessageBox mb = new MessageBox(icon, style);
		mb.setMessage(message);
		mb.setTitle(title);
		mb.setText(title);
		mb.setAnimate(true);
		mb.setBlinkModal(true);
		mb.setBorders(true);
		mb.setVisible(true);
		return mb;
	}

	public static MessageBox showMessageBox(String title, int icon, int style, String message) {
		MessageBox mb = makeMessageBox(title, icon, style, message);
		mb.setCloseOnButtonClick(true);
		mb.center();
		mb.open();
		mb.show();
		return mb;
	}

	public static void notifyCloseApp(String title, String message, final String redirectURL) {
		MessageBox mb = makeMessageBox(title, Style.ICON_ERROR, Style.MODAL, message);
		mb.setResizeEnabled(true);
		mb.setHeight(300);
		mb.setWidth(400);
		mb.addButton(new WitsButton("OK", new SelectionListener() {

			public void widgetSelected(BaseEvent be) {
				BrowserUtil.redirectBrowser(redirectURL);
			}
		}));

		MainPanel main = AppContext.getMainPanel();
		main.removeAll();
		main.hideLogout();
		main.setLayout(new CenterLayout());
		main.add(mb);
		main.layout(true);

		mb.open();
	}
}
